import requests
from lxml import etree
import pymongo
import time
class get_ip:
    def __init__(self):
        _mongo = pymongo.MongoClient(host="127.0.0.1",port=27017)
        db = _mongo.IPS
        self.ip_table = db["ip_table"]

    def get_html(self,page):

        headers = {
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Referer': 'http://www.66ip.cn/index.html',
            'Accept-Language': 'zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7',
        }

        response = requests.get(f'http://www.66ip.cn/{page}.html', headers=headers, verify=False)
        html = etree.HTML(response.text)
        trs = html.xpath('//div[@id="main"]//table//tr')
        # print(trs)
        for tr in trs:
            ip = tr.xpath('td[1]/text()')[0]
            port = tr.xpath('td[2]/text()')[0]
            if "." not in ip or "ip" in ip:
                print(f"不是ip {ip}:{port}")
                continue
            print(f"{ip}:{port}",end=" ")
            _find = self.ip_table.find_one({"ip":ip,"post":port})
            # 去重
            if not _find:
                print("ip不存在")
                self.ip_table.insert_one({"ip":ip,"post":port})
            else:
                print("ip已存在")
        
g = get_ip()
for i in range(1,2266):
    g.get_html(i)
    time.sleep(5)